package com.example.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.dao.Role;
import com.example.dao.User;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * 功能描述
 * 角色管理mapper
 * @author: jimmy
 * @date: 2023年12月22日 10:57
 */
@Repository
public interface RoleMapper extends BaseMapper<Role > {
    /**
     * 根据用户id查询角色
     * @param user
     * @return
     */
    @Select("<script>"+
            "select " +
            "roles.id,roles.role_name authority,roles.display_name displayName " +
            " from " +
            "sys_users_roles uroles,sys_roles roles " +
            "where " +
            "uroles.user_id=#{id} and uroles.role_id=roles.id " +
            "</script>")
    List<Role> findRolesByUser(User user);

    /**
     * 根据相关条件查询角色总数
     * @param role
     * @return
     */
    @Select("<script>"+
            "select count(1) total " +
            "from " +
            "sys_roles " +
            "<trim prefix=\"where\" prefixOverrides=\"and\">" +
            "    <if test=\"authority!=null and authority!=''\">" +
            "        and role_name like concat('%',#{authority},'%')" +
            "    </if>" +
            "    <if test=\"displayName!=null and displayName!=''\">" +
            "        and display_name like  concat('%',#{displayName},'%')" +
            "    </if>" +
            "</trim>" +
            "</script>")
    Integer findCount(Role role);

    /**
     * 根据相关条件分页查询
     * @param role
     * @return
     */
    @Select("<script>"+
            "select id,role_name as authority," +
            "display_name as displayName " +
            "from " +
            "  sys_roles " +
            "<trim prefix=\"where\" prefixOverrides=\"and\">" +
            "    <if test=\"authority!=null and authority!=''\">" +
            "        and role_name like concat('%',#{authority},'%')" +
            "    </if>" +
            "    <if test=\"displayName!=null and displayName!=''\">" +
            "        and display_name like  concat('%',#{displayName},'%')" +
            "    </if>" +
            "</trim>" +
            "LIMIT #{startIndex},#{pageSize};" +
            "</script>")
    List<Role> findUsersByPage(Role role);

    /**
     * 根据角色名称查询总数，检查名称是否重复
     * @param param
     * @return
     */
    @Select("<script>"+
            "select count(1) total" +
            " from " +
            "  sys_roles" +
            "<trim prefix=\"where\" prefixOverrides=\"and\">" +
            " role_name=#{rolename}" +
            " <if test=\"id!=0\">" +
            "   and id!=#{id}" +
            "</if>" +
            "</trim>" +
            "</script>")
    int findCountByRolename(Map<String, Object> param);

    /**
     * 根据用户id查询分配给该用户的角色id，
     * @param userId
     * @return
     */
    @Select("<script>"+
            "select role_id " +
            " from " +
            "sys_users_roles " +
            "where " +
            "user_id=#{userId}" +
            "</script>")
    List<Long> findRoleIdsByUserId(Long userId);

//    @Select("<script>"+
//            "select id,role_name as authority " +
//            "from " +
//            "sys_roles "  +
//            "</script>")
//    List<Role> findAllRole();
}
